const Clutter     = imports.gi.Clutter;
const Mx          = imports.gi.Mx;
const Octopus     = imports.gi.Octopus;
const Lang        = imports.lang;
const Signals     = imports.signals;
const BrowsePanel     = imports.browsePanel;
const TracklistPanel  = imports.tracklistPanel;

function MedialibPanel(container) {
	this._init(container);
}

MedialibPanel.prototype = {
	_init: function (container) {
		this.medialibPanelStack = new Mx.Stack ({ name: 'medialibPanel-background',
		                                          x_align: Clutter.ActorAlign.FILL,
		                                          y_align: Clutter.ActorAlign.FILL });
		container.add_child (this.medialibPanelStack);

		//browsePanel
		this.browsePanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
		                                        y_align: Clutter.ActorAlign.FILL });
		this.medialibPanelStack.add_child (this.browsePanelFrame); 
		this.browsePanel = new BrowsePanel.BrowsePanel (this.browsePanelFrame);
		//tracklistPanel
		this.tracklistPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
		                                           y_align: Clutter.ActorAlign.FILL });
		this.medialibPanelStack.add_child (this.tracklistPanelFrame);
		this.keyFocusActor = this.browsePanel;

		this.medialibPanelOverlay = new Mx.Frame ({ name: 'medialibPanel-overlay' });
		this.medialibPanelStack.add_child (this.medialibPanelOverlay);

		//connect to signals
		this.browsePanel.connect('load-finished', Lang.bind(this, function () {
			this.emit ('load-finished');
		}));
		//signals browsePanel
		this.browsePanel.connect('grab-key-focus', Lang.bind(this, function () {
			this.keyFocusActor = this.browsePanel;
			this.emit ('grab-key-focus');
		}));
		this.browsePanel.connect('tracklist-panel-show', Lang.bind(this, function (browsePanel, /*Album*/album_model) {
			this.tracklistPanel = new TracklistPanel.TracklistPanel (this.tracklistPanelFrame, album_model);
			this.tracklistPanel.connect('grab-key-focus', Lang.bind(this, function () {
				this.keyFocusActor = this.tracklistPanel;
				this.emit ('grab-key-focus');
			}));
			this.tracklistPanel.connect('tracklist-panel-close', Lang.bind(this, function () {
				this.keyFocusActor = this.browsePanel;
				this.emit ('grab-key-focus');
			}));
			this.tracklistPanel.connect('show-message-bar-text', Lang.bind(this, function (tracklistPanel, msg) {
				this.emit ('show-message-bar-text', msg);
			}));
			this.tracklistPanel.tracklistPanelShow ();
		}));
		this.browsePanel.connect('show-message-bar-text', Lang.bind(this, function (browsePanel, msg) {
			this.emit ('show-message-bar-text', msg);
		}));
	},

	load: function () {
		this.browsePanel.load ();
	}
};

Signals.addSignalMethods(MedialibPanel.prototype);
